import { Message, Modal } from '@arco-design/web-vue';
import { defineStore } from 'pinia';
import { computed, reactive, toRefs } from 'vue';
import { auditsData, delData, savetData } from './api';
export const useStudentStore = defineStore('student', () => {
  const model = () => ({
    username: ``,
    password: ``,
    nickname: ``,
    avatar: ``,
    birthday: ``,
    gender: ``,
    squad_id: ``,
  });
  const state = reactive({
    import: {
      template: ``,
      status: false,
      current: 1,
      click() {
        state.import.status = true;
      },
      file: undefined,
      import(e) {
        console.log(e);
        if (e.length) {
          const file = new FormData();
          file.append('file', e[0].file, e[0].name);
          state.import.file = file;
          // state.import.file = e[0].file;
        } else {
          state.import.file = undefined;
        }
      },
      async cm() {
        if (state.import.file == undefined) {
          Message.warning('请先选择文件');
          return;
        } else {
          console.log(state.import.file.prototype);
          try {
            await importData(state.import.file);
            Message.success('导入成功');
            state.fetchData();
            state.import.status = false;
          } catch (error) {
          } finally {
          }
        }
      },
      onPrev() {
        state.import.current = Math.max(1, state.import.current - 1);
      },
      onNext() {
        state.import.current = Math.min(3, state.import.current + 1);
      },
      setCurrent(current) {
        state.import.current = current;
      },
    },
    active: 3,
    status: false,
    formRef: null,
    rules: {
      username: [{ required: true, message: '请输入手机号' }],
      password: computed(() =>
        !state.form?.id ? [{ required: true, message: '请输入密码' }] : []
      ),
      nickname: [{ required: true, message: '请输入姓名' }],
      avatar: [{ required: true, message: '请上传照片' }],
      birthday: [{ required: true, message: '请选择出生日期' }],
      gender: [{ required: true, message: '请选择性别' }],
      squad_id: [{ required: true, message: '请选择班级' }],
    },
    options: {
      squad: [],
      audits: [
        {
          label: '已审核',
          value: 3,
          count: 0,
        },
        { label: '待审核', value: 2, count: 0 },
        { label: '已驳回', value: 4, count: 0 },
      ],
    },
    rowSelection: {
      showCheckedAll: true,
      type: 'checkbox',
    },
    selectedKeys: [],
    form: model(),
    fetchData: null,
    allAudits(status) {
      const ids = state.selectedKeys.join(',');
      if (!ids) {
        Message.warning('请选择要审核学员');
        return;
      }
      Modal.error({
        title: '操作提示',
        content: `是否确认${
          status == 3 ? '通过' : status == 4 ? '驳回' : ''
        }所选学员?`,
        async onOk() {
          await state.audits(ids, status);
        },
      });
    },
    async audits(id, status) {
      await auditsData({ ids: id, status });
      Message.success(status == 3 ? '通过成功' : status == 4 ? '驳回成功' : '');
      state.fetchData();
    },
    async handleBeforeOk(done) {
      const vaild = !!(await state.formRef.validate());
      if (vaild) {
        done(false);
        console.error(state.form);
        return;
      }
      console.log('success');
      await savetData(state.form);
      Message.success(state.form?.id ? '编辑成功' : '新建成功');
      state.status = false;
      state.fetchData();
    },
    open(focusRef, formRef) {
      focusRef.focus();
      state.formRef = formRef;
    },
    async add() {
      state.status = true;
      state.form = model();
    },
    async dels() {
      const ids = state.selectedKeys.join(',');
      console.log(ids);
      if (!ids) {
        Message.warning('请选择要删除的学员');
        return;
      }
      Modal.error({
        title: '操作提示',
        content: '是否确认删除所选学员?',
        async onOk() {
          await state.del(ids);
        },
      });
    },
    async del(id) {
      await delData(id);
      Message.success('删除成功');
      state.fetchData();
    },
    edit(record) {
      console.log(record);
      Object.assign(state.form, {
        ...record,
      });
      state.status = true;
    },
  });
  return {
    ...toRefs(state),
  };
});

export default useStudentStore;
